package com.yylany.service;

import com.yylany.bo.User;
import com.yylany.mapper.TbUserMapper;
import com.yylany.pojo.TbUser;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description: 用户的service，对user表进行操作
 *
 * @author <a href="mailto:17777502615@163.com">yaoyonglong</a>
 * @date 2018/12/17
 * @since 1.0.0
 */
@Service
@Transactional
public class UserService {

    /**
     * 生成id的工具
     */
    private Sid sid;

    /**
     * 这个是user的dao
     */
    private TbUserMapper userMapper;

    @Autowired
    public UserService(Sid sid, TbUserMapper userMapper) {
        this.sid = sid;
        this.userMapper = userMapper;
    }

    /**
     * 保存用户信息
     *
     * @param user
     */
    public void saveUser(User user) {

        TbUser tbUser = new TbUser();
        BeanUtils.copyProperties(user, tbUser);
        userMapper.insert(tbUser);


    }

    /**
     * 查询所有用户信息
     *
     * @return
     */
    public List<TbUser> select() {
        return userMapper.selectAll();
    }

    /**
     * 根据 mobile  查询
     *
     * @param mobile
     * @return 返回该mobile 的对象
     */
    public TbUser findUserByMobile(String mobile) {

/**
 *          Example example = new Example(TbUser.class);
 *         Example.Criteria criteria = example.createCriteria();
 *         criteria.andEqualTo("name", name);
 *         TbUser tbUser = userMapper.selectOneByExample(example);
 */
        TbUser tbUser = userMapper.selectByMobile(mobile);

        return tbUser;
    }
}
